// mircoApp/communityMarket/search/search.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    activeid: 0,
    kwd: '',
    point: {},
    list: [],
    page: 1,
    total_page: 1,
    isLoadMore: true,
    loading: true,

    addressShow: false,
    address: '',
    pickerValue: [0, 0, 0],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      activeid: options.storeid||0
    });
    this.initData();
    this._getLocation((mapdata, e) => {
      this.data.point = {
        lng: e.lng,
        lat: e.lat
      };
      this.setData({
        address: mapdata.address_component.city
      });
      this.getData();
    });
    this._initAddressInfo();
  },
  initData(){
    this.data.current_page = 1;
    this.data.isLoadMore = true;
    this.data.total_page = 1;
    this.setData({
      list: []
    });
  },
  //获取数据
  getData() {
    let urlStr = app.getNetAddresss('plugin.community-marketing.frontend.controllers.index.changeStore');
    app._postNetWork({
      url: urlStr,
      data: {
        name: this.data.kwd,
        lng: this.data.point.lng,
        lat: this.data.point.lat,
        page: this.data.page,
        city_name: this.data.address
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.data.current_page = res.data.list.current_page;
          this.data.total_page = res.data.list.last_page;
          this.setData({
            list: res.data.list.data || [],
            loading: false
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  //加载更多数据
  _getMoreData(){
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss('plugin.community-marketing.frontend.controllers.index.changeStore');
      app._postNetWork({
        url: urlStr,
        data: {
          name: this.data.kwd,
          lng: this.data.point.lng,
          lat: this.data.point.lat,
          page: this.data.page,
          city_name: this.data.address
        },
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let list = this.data.list.concat(res.data.list.data);
            this.setData({list});
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },
  onClickStore(e){
    let id = e.currentTarget.dataset.id;
    wx.redirectTo({
      url: "/mircoApp/communityMarket/index/index?mode=qrStore&is_alone=0&chg=1&store_id=" + id,
      fail: ()=> {
        wx.reLaunch({
          url: "/mircoApp/communityMarket/index/index?mode=qrStore&is_alone=0&chg=1&store_id=" + id,
        });
      }
    });
  },
  onChange(e) {
    this.setData({
      kwd: e.detail,
    });
  },
  onSearch(){
    this.initData();
    this.getData();
  },
  onChangeCity(){
    this.setData({
      addressShow: true
    });
  },

  _closeDateLw() {
    this.setData({
      addressShow: false,
    });
  },
  resultAdd() {
    let seleData = this.data.selectAddressData;
    let retCity =
      seleData[0].name + " " + seleData[1].name + " " + seleData[2].name;
    this.setData({
      address: seleData[1].name,
      addressShow: false,
      province_id: seleData[0].id,
      city_id: seleData[1].id,
      district_id: seleData[2].id,
    });
    this.initData();
    this.getData();
  },
  //地址选择器数据初始化
  _initAddressInfo: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("地址信息准备就绪");
        that._getProvinceData();
      },
      fail: function (res) {
        console.log("省fail");
        app._getNetWork({
          url: app.getNetAddresss("member.member-address.address"),
          success: function (res) {
            var response = res.data;
            var province = response.data.province;
            var city = response.data.city;
            var district = response.data.district;
            wx.setStorage({
              key: "provinceData",
              data: province,
            });
            wx.setStorage({
              key: "cityData",
              data: city,
            });
            wx.setStorage({
              key: "districtData",
              data: district,
            });
            that._initAddressInfo();
          },
          fail: function (res) {},
          state: true,
        });
      },
    });
  },
  // 读取省key
  _getProvinceData: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        that.setData({
          provinceData: res.data,
        });
        that.setData({
          "selectAddressData[0].name": res.data[that.data.pickerValue[0]].areaname,
          "selectAddressData[0].id": res.data[that.data.pickerValue[0]].id,
          "selectAddressData[0].index": that.data.pickerValue[0],
        });
        that._getCityData();
      },
    });
  },
  // 读取城市key，筛选城市data
  _getCityData: function () {
    var _data = this.data.selectAddressData[0];
    var that = this;
    var _keyName = "cityData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        that.setData({
          cityData: _json,
        });
        that.setData({
          "selectAddressData[1].name": _json[that.data.pickerValue[1]].areaname,
          "selectAddressData[1].id": _json[that.data.pickerValue[1]].id,
          "selectAddressData[1].index": that.data.pickerValue[1],
        });
        that._getDistrictData();
      },
    });
  },
  // 读取地区key，筛选地区data
  _getDistrictData: function () {
    var _data = this.data.selectAddressData[1];
    var that = this;
    var _keyName = "districtData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        that.setData({
          districtData: _json,
        });
        that.setData({
          "selectAddressData[2].name": _json[that.data.pickerValue[2]].areaname,
          "selectAddressData[2].id": _json[that.data.pickerValue[2]].id,
          "selectAddressData[2].index": that.data.pickerValue[2],
        });
      },
    });
  },
  // 收货地址滚动事件
  bindChange: function (e) {
    var val = e.detail.value;
    if (this.data.pickerValue[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
    }
    if (this.data.pickerValue[1] != val[1]) {
      val[2] = 0;
    }
    this.setData({
      "pickerValue[0]": val[0],
      "pickerValue[1]": val[1],
      "pickerValue[2]": val[2],
    });
    this._initAddressInfo();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
})